package org.domain.sample.session;

import java.security.NoSuchAlgorithmException;
import javax.persistence.EntityManager;
import java.security.MessageDigest;

import org.domain.sample.entity.Users;
import org.jboss.seam.annotations.In;

import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;


@Name("authenticator")
public class Authenticator {
	@Logger
	private Log log;

	@In
	Identity identity;
	@In
	Credentials credentials;
	@In
	EntityManager entityManager;

	@Out
	Users user;

	@SuppressWarnings("deprecation")
	public boolean authenticate() {
		log.info("authenticatin {0}", credentials.getUsername());
		
		//Retrieving User whose login name matches
		Users user = (Users) entityManager
				.createQuery(
						"SELECT users FROM Users users WHERE users.name = :userName")
				.setParameter("userName", identity.getUsername())
				.getSingleResult();
		this.user = user;

		//User does not exist
		if (user == null) {
			log.info("No such user " + identity.getUsername());
			return false;
		}
		//User Exists
		log.info("Yes such user " + identity.getUsername());
		if(identity.getPassword().equals(user.getPassword()))
		{
			log.info("Yes such password " + identity.getUsername());
			log.info("initializing mapinstance for"
					+ identity.getUsername());

			MapInstanceManager.instance().setMapInstance(
					identity.getUsername());
			MapInstance mapInstance = MapInstanceManager.instance()
					.getMapInstance(identity.getUsername());
			mapInstance.setMuted(false);
			mapInstance.setMediaConnected(false);
			//mapInstance.setPushServerMessage(new PushServerMessage());
			mapInstance.setStatusMessage("No incoming calls for "
					+ credentials.getUsername());

			return true;
		}
		/*		String plainText = identity.getPassword();
		try {
			MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
			mdAlgorithm.update(plainText.getBytes());

			byte[] digest = mdAlgorithm.digest();
			StringBuffer hexString = new StringBuffer();

			for (int i = 0; i < digest.length; i++) {
				plainText = Integer.toHexString(0xFF & digest[i]);

				if (plainText.length() < 2) {
					plainText = "0" + plainText;
				}
				hexString.append(plainText);
			}

			if (user.getPassword().equals(hexString.toString())) {
				identity.addRole("basic");
				log.info("Yes such password " + identity.getUsername());
				
				 * creating an entry into the hash table with username as the
				 * hashkey
				 

<<<<<<< .mine
				log.info("initializing mapinstance for"
						+ identity.getUsername());

				/* instantiating mapinstance class for this user */
				MapInstanceManager.instance().setMapInstance(
						identity.getUsername());
				MapInstance mapInstance = MapInstanceManager.instance()
						.getMapInstance(identity.getUsername());
				mapInstance.setMuted(false);
				mapInstance.setMediaConnected(false);
				mapInstance.setPushServerMessage(new PushServerMessage());
				mapInstance.setUserStatus(UserStatus.NEITHER);
				mapInstance.setStatusMessage("No incoming calls for "
						+ credentials.getUsername());
				/*
				 * if(user.getRoles() != null) { for(Role role: user.getRoles())
				 * { identity.addRole(role.getRole()); } }
				 */
				return true;
=======
								return true;
>>>>>>> .r26
			}
			return false;

		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();
		}*/
		return false;

	}
}
